//
//  ViewController.m
//  Ruique
//
//  Created by xiute on 2018/10/26.
//  Copyright © 2018 cuijin. All rights reserved.
//

#import "ViewController.h"
#import "NetService.h"
#import "RuiQueUserInterface/RuiQueViewModel.h"
#import "RuiQueUserInterface/SearchCollectionViewCell.h"


static NSString * const kSearchCollectionCellIndentifier = @"kSearchCollectionCellIndentifier";
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) RuiQueViewModel *viewModel;
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.collectionView];
    [self refreshData];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)refreshData {
    __weak typeof(self)weakSelf = self;
    [self.viewModel refreshData:^{
        __strong typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf updateTabeleViewUI];
        [strongSelf.tableView.mj_header endRefreshing];
        //如果有错误页面 这里可以加上错误页面的逻辑
        
    }];
}

- (void)updateTabeleViewUI {
    [self.tableView reloadData];
    [self.collectionView reloadData];
}

#pragma mark -collectionview delegate and datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.viewModel.searchValue.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kSearchCollectionCellIndentifier forIndexPath:indexPath];
    
    cell.currencyName.text = self.viewModel.searchValue[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    [self.viewModel searchUnit:self.viewModel.searchValue[indexPath.row]];
    [self updateTabeleViewUI];
}

#pragma mark -properties
- (UITableView *)tableView {
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNavbarHeight, kScreenWidth, kScreenHeight - kNavbarHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak typeof(self)weakSelf = self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong typeof(weakSelf)strongSelf = weakSelf;
            [strongSelf refreshData];
        }];
        [self.viewModel registTableView:_tableView];
    }
    return _tableView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(44, 44);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth, 44) collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerNib:[UINib nibWithNibName:kCollectionCellNibName bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kSearchCollectionCellIndentifier];
    }
    return _collectionView;
}

- (RuiQueViewModel *)viewModel {
    if (!_viewModel) {
        self.viewModel = [[RuiQueViewModel alloc]init];
    }
    return _viewModel;
}

@end
